<!--
Copyright 2007, Google Inc.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

 1. Redistributions of source code must retain the above copyright notice, 
    this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
 3. Neither the name of Google Inc. nor the names of its contributors may be
    used to endorse or promote products derived from this software without
    specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->

<!DOCTYPE html>

<html>
<head>
<title>Google Gears Database Demo</title>
<link rel="stylesheet" type="text/css" href="sample.css">
</head>

<body onload="displayRecentPhrases()">
<h1>Google Gears Database Demo</h1>

<form onsubmit="handleSubmit(); return false;">
  <b>Enter a phrase to store in the database:</b>&nbsp;<br>
  <table>
    <tr>
      <td valign="middle"><input type="text" id="submitValue" style="width:20em;"></td>
      <td valign="middle"><input type="submit" value="OK"></td>
    </tr>
  </table>
</form>

<p><b>Your last three phrases were:</b>

<p><span id="status">&nbsp;</span>
  
<p><i>This page uses Google Gears to record your entries on the local disk.
   If you navigate away and revisit this page, all your data will still
   be here.  Try it!</i>

<!-- ====================================== -->
<!-- End HTML code.  Begin JavaScript code. -->

<script type="text/javascript" src="gears_init.js" runat="server"></script>
<script type="text/javascript" runat="server">
function getDB() {
//  if (window.google && google.gears) {
    try {
      var db = google.gears.factory.create('beta.database', '1.0');

      if (db) {
        db.open('database-demo');
        db.execute('create table if not exists Demo' +
                   ' (Phrase varchar(255), Timestamp int)');
      }
      return db;
    } catch (ex) {
      throw ex.message;
    }
//  }
}

function getRecentPhrases() {
  var recentPhrases = ['', '', ''];
  var db = getDB();
    
  // We re-throw Gears exceptions to make them play nice with certain tools.
  // This will be unnecessary in a future version of Gears.
  try {
    // Get the 3 most recent entries. Delete any others.
    var rs = db.execute('select * from Demo order by Timestamp desc');
    var index = 0;
    while (rs.isValidRow()) {
      if (index < 3) {
        recentPhrases[index] = rs.field(0);
      } else {
        db.execute('delete from Demo where Timestamp=?', [rs.field(1)]);
      }
      ++index;
      rs.next();
    }
    rs.close();

  } catch (e) {
    throw new Error(e.message);
  }

  return recentPhrases;
}
getRecentPhrases.proxy = true;

function addPhrase(phrase, currTime) {
  getDB().execute('insert into Demo values (?, ?)', [phrase, currTime]);
}
addPhrase.proxy = true;

</script>

<script type="text/javascript">
function handleSubmit() {
  var elm = document.getElementById('submitValue');
  var phrase = elm.value;
  var currTime = new Date().getTime();

  // Insert the new item.
  // The Gears database automatically escapes/unescapes inserted values.
  addPhrase(phrase, currTime);

  // Update the UI.
  elm.value = '';
  displayRecentPhrases();
}

function displayRecentPhrases() {
  var recentPhrases = getRecentPhrases();
  
  var status = document.getElementById('status');
  status.innerHTML = '';
  for (var i = 0; i < recentPhrases.length; ++i) {
    var bullet = '(' + (i + 1) + ') ';
    status.appendChild(document.createTextNode(bullet + recentPhrases[i]));
    status.appendChild(document.createElement('br'));
  }
}
</script>

</body>
</html>
